package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.entity.SkuInfo;
import com.atguigu.gmall.product.entity.SpuSaleAttr;
import com.atguigu.gmall.product.service.SkuInfoService;
import com.atguigu.gmall.product.service.SkuSaleAttrValueService;
import com.atguigu.gmall.product.service.SpuSaleAttrValueService;
import com.atguigu.gmall.product.vo.SkuDataVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author lfy
 * @Description
 * @create 2023-07-14 11:07
 */
@RequestMapping("/admin/product")
@RestController
public class SkuRestController {

    @Autowired
    SkuInfoService skuInfoService;

    @Autowired
    SpuSaleAttrValueService spuSaleAttrValueService;

    /**
     * sku列表
     *
     * @param pn
     * @param ps
     * @return
     */
    @GetMapping("/list/{pn}/{ps}")
    public Result list(@PathVariable("pn") Long pn,
                       @PathVariable("ps") Long ps) {

        Page<SkuInfo> page = skuInfoService.lambdaQuery()
                .page(new Page<>(pn, ps));

        return Result.ok(page);
    }


    /**
     * spu销售属性名和值
     *
     * @param spuId
     * @return
     */
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(@PathVariable("spuId") Long spuId) {
        /**
         * select ssa.*,ssav.id vid,ssav.sale_attr_value_name
         * from spu_sale_attr ssa
         * left join spu_sale_attr_value ssav
         *   on ssav.spu_id=ssa.spu_id
         * 	and ssav.base_sale_attr_id = ssa.base_sale_attr_id
         * where ssa.spu_id=28
         * order by ssa.base_sale_attr_id,ssav.id
         */
        //获取当前spu所有销售属性名和其对应的所有值
        List<SpuSaleAttr> spuSaleAttrs = spuSaleAttrValueService
                .getSpuSaleAttrList(spuId);

        return Result.ok(spuSaleAttrs);
    }

    /**
     * 保存sku信息
     *
     */
    @PostMapping("/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody @Validated  SkuDataVo skuData) {
        skuInfoService.checkSkuSaveData(skuData);

        skuInfoService.saveSkuInfo(skuData);

        return Result.ok();
    }

    /**
     * 上下架
     */
    @GetMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable("skuId") Long skuId) {
        skuInfoService.onSale(skuId);
        return Result.ok();
    }

    @GetMapping("/cancelSale/{skuId}")
    public Result cancleSale(@PathVariable("skuId") Long skuId) {
        skuInfoService.cancleSale(skuId);
        return Result.ok();
    }


    /**
     * 商品修改
     * @return
     */
    public Result updateSku() {
        //1、修改数据库

        //2、删除缓存（2次）

        return Result.ok();
    }

}
